package org.webcat.electronicsubmissionplugin.options;
import java.net.MalformedURLException;
import java.net.URL;
import org.webcat.electronicsubmissionplugin.model.SubmitterModel;

/**
 * Visual implementation of the menu options tab.
 * @author rob44, rstodd13, samccarn
 */
final class ElectronicSubmissionPanel extends javax.swing.JPanel {

    private final ElectronicSubmissionOptionsPanelController controller;
    private SubmitterModel model;
    private boolean isValid;

    /**
     * Create a new ElectronicSubmissionPanel object.
     * @param controller the controller
     */
    ElectronicSubmissionPanel(ElectronicSubmissionOptionsPanelController controller) {
        this.controller = controller;
        model = SubmitterModel.getInstance();
        initComponents();
        errorText.setVisible(false);
        isValid = false;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        instructionsPanel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        emailAddressPanel = new javax.swing.JPanel();
        emailTextField = new javax.swing.JTextField();
        mailServerPanel = new javax.swing.JPanel();
        mailServerTextField = new javax.swing.JTextField();
        assignmentDefinitionPanel = new javax.swing.JPanel();
        urlTextField = new javax.swing.JTextField();
        usernamePanel = new javax.swing.JPanel();
        usernameTextField = new javax.swing.JTextField();
        errorText = new javax.swing.JLabel();

        instructionsPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(org.openide.util.NbBundle.getMessage(ElectronicSubmissionPanel.class, "ElectronicSubmissionPanel.instructionsPanel.border.title"))); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(ElectronicSubmissionPanel.class, "ElectronicSubmissionPanel.jLabel1.text")); // NOI18N

        javax.swing.GroupLayout instructionsPanelLayout = new javax.swing.GroupLayout(instructionsPanel);
        instructionsPanel.setLayout(instructionsPanelLayout);
        instructionsPanelLayout.setHorizontalGroup(
            instructionsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(instructionsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 481, Short.MAX_VALUE)
                .addContainerGap())
        );
        instructionsPanelLayout.setVerticalGroup(
            instructionsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(instructionsPanelLayout.createSequentialGroup()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        emailAddressPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(org.openide.util.NbBundle.getMessage(ElectronicSubmissionPanel.class, "ElectronicSubmissionPanel.emailAddressPanel.border.title"))); // NOI18N

        emailTextField.setText(org.openide.util.NbBundle.getMessage(ElectronicSubmissionPanel.class, "ElectronicSubmissionPanel.emailTextField.text")); // NOI18N

        javax.swing.GroupLayout emailAddressPanelLayout = new javax.swing.GroupLayout(emailAddressPanel);
        emailAddressPanel.setLayout(emailAddressPanelLayout);
        emailAddressPanelLayout.setHorizontalGroup(
            emailAddressPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(emailAddressPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(emailTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 481, Short.MAX_VALUE)
                .addContainerGap())
        );
        emailAddressPanelLayout.setVerticalGroup(
            emailAddressPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, emailAddressPanelLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(emailTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        mailServerPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(org.openide.util.NbBundle.getMessage(ElectronicSubmissionPanel.class, "ElectronicSubmissionPanel.mailServerPanel.border.title"))); // NOI18N

        mailServerTextField.setText(org.openide.util.NbBundle.getMessage(ElectronicSubmissionPanel.class, "ElectronicSubmissionPanel.mailServerTextField.text")); // NOI18N

        javax.swing.GroupLayout mailServerPanelLayout = new javax.swing.GroupLayout(mailServerPanel);
        mailServerPanel.setLayout(mailServerPanelLayout);
        mailServerPanelLayout.setHorizontalGroup(
            mailServerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mailServerPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(mailServerTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 481, Short.MAX_VALUE)
                .addContainerGap())
        );
        mailServerPanelLayout.setVerticalGroup(
            mailServerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mailServerPanelLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(mailServerTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        assignmentDefinitionPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(org.openide.util.NbBundle.getMessage(ElectronicSubmissionPanel.class, "ElectronicSubmissionPanel.assignmentDefinitionPanel.border.title"))); // NOI18N

        urlTextField.setText(org.openide.util.NbBundle.getMessage(ElectronicSubmissionPanel.class, "ElectronicSubmissionPanel.urlTextField.text")); // NOI18N
        urlTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                urlTextFieldKeyReleased(evt);
            }
        });

        javax.swing.GroupLayout assignmentDefinitionPanelLayout = new javax.swing.GroupLayout(assignmentDefinitionPanel);
        assignmentDefinitionPanel.setLayout(assignmentDefinitionPanelLayout);
        assignmentDefinitionPanelLayout.setHorizontalGroup(
            assignmentDefinitionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(assignmentDefinitionPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(urlTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 481, Short.MAX_VALUE)
                .addContainerGap())
        );
        assignmentDefinitionPanelLayout.setVerticalGroup(
            assignmentDefinitionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, assignmentDefinitionPanelLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(urlTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        usernamePanel.setBorder(javax.swing.BorderFactory.createTitledBorder(org.openide.util.NbBundle.getMessage(ElectronicSubmissionPanel.class, "ElectronicSubmissionPanel.usernamePanel.border.title"))); // NOI18N

        usernameTextField.setText(org.openide.util.NbBundle.getMessage(ElectronicSubmissionPanel.class, "ElectronicSubmissionPanel.usernameTextField.text")); // NOI18N

        javax.swing.GroupLayout usernamePanelLayout = new javax.swing.GroupLayout(usernamePanel);
        usernamePanel.setLayout(usernamePanelLayout);
        usernamePanelLayout.setHorizontalGroup(
            usernamePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(usernamePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(usernameTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 481, Short.MAX_VALUE)
                .addContainerGap())
        );
        usernamePanelLayout.setVerticalGroup(
            usernamePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, usernamePanelLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(usernameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        errorText.setFont(new java.awt.Font("Tahoma", 1, 11));
        errorText.setForeground(new java.awt.Color(255, 0, 0));
        errorText.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        org.openide.awt.Mnemonics.setLocalizedText(errorText, org.openide.util.NbBundle.getMessage(ElectronicSubmissionPanel.class, "ElectronicSubmissionPanel.errorText.text")); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(errorText, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 513, Short.MAX_VALUE)
                    .addComponent(instructionsPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(emailAddressPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(mailServerPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(assignmentDefinitionPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(usernamePanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(instructionsPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(5, 5, 5)
                .addComponent(emailAddressPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(mailServerPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(5, 5, 5)
                .addComponent(assignmentDefinitionPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(usernamePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(errorText, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(22, 22, 22))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void urlTextFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_urlTextFieldKeyReleased
        valid();
        controller.changed();
    }//GEN-LAST:event_urlTextFieldKeyReleased



    /**
     * Uses the model to store desired user variables.
     * If nothing is there, will leave the default values.
     */
    void load() {
        String emailText = model.getEmail();
        if (emailText.length() > 0) {
            emailTextField.setText(emailText);
        }
        String mailServerText = model.getMailServer();
        if (mailServerText.length() > 0) {
            mailServerTextField.setText(mailServerText);
        }

        String urlText = model.getSubmitUrl();
        if (urlText.length() > 0) {
            urlTextField.setText(urlText);
        }

        String usernameText = model.getUsername();
        if (usernameText.length() > 0) {
            usernameTextField.setText(usernameText);
        }

        valid();
    }

    /**
     * Uses the model to load defined user variables.
     */
    void store() {
        model.setEmail(emailTextField.getText());
        model.setMailServer(mailServerTextField.getText());
        model.setSubmitUrl(urlTextField.getText());
        model.setUsername(usernameTextField.getText());
    }

    /**
     * Validate the necessary fields.
     * @return isValid
     */
    boolean valid() {
        String urlText = urlTextField.getText();
        try {
            URL testURL = new URL(urlText);
            isValid = true;
        } catch (MalformedURLException ex) {
            isValid = false;
            errorText.setText("Error: You must enter a well-formed URL.");
        }
        if (isValid) {
            errorText.setVisible(false);
        } else {
            errorText.setVisible(true);
        }
        return isValid;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel assignmentDefinitionPanel;
    private javax.swing.JPanel emailAddressPanel;
    private javax.swing.JTextField emailTextField;
    private javax.swing.JLabel errorText;
    private javax.swing.JPanel instructionsPanel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel mailServerPanel;
    private javax.swing.JTextField mailServerTextField;
    private javax.swing.JTextField urlTextField;
    private javax.swing.JPanel usernamePanel;
    private javax.swing.JTextField usernameTextField;
    // End of variables declaration//GEN-END:variables
}
